package com.tpc.pictool.utils;


import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
public class JDBCUtil {
    private static DruidDataSource dataSource = null;

    public JDBCUtil() {
    }

    public static Connection getConnection() throws SQLException {
        Connection conn = dataSource.getConnection();
        return conn;
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException var2) {
                var2.printStackTrace();
            }

            conn = null;
        }

    }

    public static void close(Statement stmt, Connection conn) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException var4) {
                var4.printStackTrace();
            }

            stmt = null;
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException var3) {
                var3.printStackTrace();
            }

            conn = null;
        }

    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException var6) {
                var6.printStackTrace();
            }

            rs = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException var5) {
                var5.printStackTrace();
            }

            stmt = null;
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException var4) {
                var4.printStackTrace();
            }

            conn = null;
        }

    }

    public static String getTableColumn(String tableName) {
        Connection connection = null;
        DatabaseMetaData metadata = null;
        String nameColumn = "";

        try {
            connection = getConnection();
            metadata = connection.getMetaData();

            String name;
            for(ResultSet resultSet = metadata.getColumns((String)null, (String)null, tableName.toUpperCase(), (String)null); resultSet.next(); nameColumn = nameColumn + name.toLowerCase() + ",") {
                name = resultSet.getString("COLUMN_NAME");
            }
        } catch (SQLException var9) {
            var9.printStackTrace();
        } finally {
            close(connection);
        }

        return nameColumn;
    }

    public static Map<String, String> getTableColumnType(String tableName) throws SQLException {
        Connection connection = getConnection();
        DatabaseMetaData metadata = null;
        Map<String, String> map = new HashMap();

        try {
            metadata = connection.getMetaData();
            ResultSet resultSet = metadata.getColumns((String)null, (String)null, tableName.toUpperCase(), (String)null);

            while(resultSet.next()) {
                String name = resultSet.getString("COLUMN_NAME").toLowerCase();
                String nameType = resultSet.getString("TYPE_NAME");
                map.put(name, nameType);
            }
        } catch (SQLException var10) {
            var10.printStackTrace();
        } finally {
            close(connection);
        }

        return map;
    }

    static {
        String filePath = null;

        try {
            String dbType = "ORACLE";
            String server = "121.36.18.120";
            String userName = "SKZYWMS";
            String passWord = "Sfrx2019";
            String dataBase = "SKZY";
            String port = "1521";
            String driver = "";
            String url = "";
            if (dbType.equals("MSSQL")) {
                driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                url = "jdbc:sqlserver://" + server + ":" + port + ";DatabaseName=" + dataBase;
            } else if (dbType.equals("ORACLE")) {
                driver = "oracle.jdbc.OracleDriver";
                url = "jdbc:oracle:thin:@" + server + ":" + port + ":" + dataBase;
            } else if (dbType.equals("MYSQL")) {
                driver = "com.mysql.jdbc.Driver";
                url = "jdbc:mysql://" + server + ":" + port + "/" + dataBase;
            } else if (dbType.equals("RAC")) {
                driver = "oracle.jdbc.OracleDriver";
                url = "jdbc:oracle:thin:@" + server;
            }

            dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(userName);
            dataSource.setPassword(passWord);
            dataSource.setMinIdle(5);
            dataSource.setMaxActive(200);
            dataSource.setRemoveAbandoned(true);
            dataSource.setRemoveAbandonedTimeout(60);
            dataSource.setMaxWait(10000L);
            dataSource.setInitialSize(1);
        } catch (Exception var9) {
            var9.printStackTrace();
        }

    }
}
